package com.sendpo.zjx.sendpo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sendpo.zjx.sendpo.R;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.shizhefei.view.viewpager.SViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zjx on 2018
 */

public class MainYanchuFragment extends BaseMainFragment {
    public static String[] titles = { "附近活动", "圈里赛事", "我的圈子" };
    @BindView(R.id.indicator)
    ScrollIndicatorView indicator;
    private IndicatorViewPager indicatorViewPager;
    public View rootView;
    @BindView(R.id.viewpager)
    SViewPager viewpager;

    public View initView()
    {
        View localView = View.inflate(this.mActivity, R.layout.fragment_mainyanchu, null);
        ButterKnife.bind(localView, this.mActivity);
        return localView;
    }

    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
    {
        if (this.rootView == null)
        {
            this.rootView = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
            ButterKnife.bind(this, this.rootView);
            initData();
        }
        return this.rootView;
    }

    private void initData() {
        this.viewpager.setCanScroll(false);
        this.viewpager.setOffscreenPageLimit(2);
        this.indicatorViewPager = new IndicatorViewPager(this.indicator, this.viewpager);
        List<BaseMainFragment> arrayList = new ArrayList();
        arrayList.add(new YanchuFragment());
        arrayList.add(new QuanliSaiThingsFragment());
        arrayList.add(new MyQuanziFragment());
        this.indicatorViewPager.setAdapter(new FragmentIndicatorAdapter(getChildFragmentManager(),arrayList));
        this.indicatorViewPager.setClickIndicatorAnim(true);
        this.indicatorViewPager.setIndicatorOnTransitionListener(new OnTransitionTextListener().setColor(getResources().getColor(R.color.default_theme), getResources().getColor(R.color.gray)));
        ColorBar colorBar = new ColorBar(mActivity, getResources().getColor(R.color.default_theme), 5);
        colorBar.setWidth(120);
        indicatorViewPager.setIndicatorScrollBar(colorBar);
        this.indicatorViewPager.setIndicatorScrollBar(colorBar);
    }

    private class FragmentIndicatorAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {
        private List<BaseMainFragment> fragments;

        public FragmentIndicatorAdapter(FragmentManager fragmentManager, List<BaseMainFragment> fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = View.inflate(mActivity, R.layout.tab_focus, null);
            }
            TextView textView = (TextView) convertView;
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            textView.setText(titles[position]);
            return textView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return fragments.get(position);
        }
    }
}
